package app.server.project.cloud.random.db.entity;

import app.server.project.core.constants.Def;
import app.server.project.core.syndb.SynDBMgr;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Transient;
import lombok.Data;
import org.hibernate.annotations.Comment;

import java.time.LocalDateTime;

@Data
@Entity(name = "dev_name")
@TableName("dev_name")
@Comment(value = "设备名称信息表")
public class DevNameEntity {

    @Id
    @Column(name = "id")
    @Comment("用户主键")
    @TableId("id")
    private long id;

    @TableField("dev_id")
    @Comment("设备id")
    @Column(name = "dev_id")
    private long devId;

    @TableField("dev_ch")
    @Comment("设备支路名称")
    @Column(name = "dev_ch")
    private int devCh;



    @TableField("create_time")
    @Comment("创建时间")
    @Column(name = "create_time")
    private LocalDateTime createTime;

    @TableField("name")
    @Comment("支路名称")
    @Column(name = "name")
    private String name;



    @Transient
    @TableField(exist = false)
    private boolean synDb = false;

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(DevNameEntity::getCreateTime, this);
    }

    public void setName(String name) {
        this.name = name;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(DevNameEntity::getName, this);
    }


    public void setDevId(long devId) {
        this.devId = devId;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(DevNameEntity::getDevId, this);
    }

    public void setDevCh(int devCh) {
        this.devCh = devCh;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(DevNameEntity::getDevCh, this);
    }

    public static DevNameEntity of(long id, int devCh,long devId, String name){
        var ret=new DevNameEntity();
        ret.setSynDb(true);
        ret.setId(id);
        ret.setDevCh(devCh);
        ret.setDevId(devId);
        ret.setName(name);
        ret.setCreateTime(LocalDateTime.now());

        return ret;
    }

}
